package com.hui.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hui.handler.exception.SystemException;
import com.hui.mapper.StoreProductRelationMapper;
import com.hui.entity.dao.StoreProductRelation;
import com.hui.entity.enums.AppHttpCodeEnum;
import com.hui.entity.vo.StoreProductRelationQueryVo;
import com.hui.service.StoreProductRelationService;
import com.hui.util.ResponseResult;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 商品点赞和收藏表(StoreProductRelation)表服务实现类
 *
 * @author hui
 * @since 2022-07-17 15:20:45
 */
@RequiredArgsConstructor
@Service("storeProductRelationService")
public class StoreProductRelationServiceImpl extends ServiceImpl<StoreProductRelationMapper, StoreProductRelation> implements StoreProductRelationService {

    private final StoreProductRelationMapper storeProductRelationMapper;

    /**
     * 判断是否用户收藏该商品
     *
     * @param productId 商品ID
     * @param uid       用户ID
     * @return
     */
    @Override
    public Boolean isProductRelation(long productId, long uid) {
        LambdaQueryWrapper<StoreProductRelation> wrapper = new LambdaQueryWrapper();
        wrapper.eq(StoreProductRelation::getUid, uid)
                .eq(StoreProductRelation::getType, "collect")
                .eq(StoreProductRelation::getProductId, productId);
        long count = count(wrapper);
        return count > 0L;
    }

    @Override
    public ResponseResult addRroductRelation(Long productId, Long uid, String category) {
        if (isProductRelation(productId, uid)) {
            throw new SystemException(AppHttpCodeEnum.SUCCESS.getCode(), "已收藏");
        }
        //构造实体类
        StoreProductRelation storeProductRelation = StoreProductRelation.builder()
                .productId(productId)
                .uid(uid)
                .type(category)
                .createTime(new Date())
                .build();
        //进行插入
        boolean save = save(storeProductRelation);
        return ResponseResult.okResult();
    }


    @Override
    public ResponseResult delRroductRelation(Long productId, Long uid, String category) {
        //lambdaQuery的一种构造 （可能吧）
        //查询出来一个对象
        StoreProductRelation productRelation = this.lambdaQuery()
                .eq(StoreProductRelation::getProductId, productId)
                .eq(StoreProductRelation::getUid, uid)
                .eq(StoreProductRelation::getType, category)
                .one();
        if (productRelation == null) {
            throw new SystemException(AppHttpCodeEnum.SYSTEM_ERROR, "该商品未收藏");
        }
        removeById(productRelation.getId());
        return ResponseResult.okResult();
    }

    @Override
    public List<StoreProductRelationQueryVo> userCollectProduct(Integer page, Integer limit, Long uid, String type) {
        Page<StoreProductRelation> pageModel = new Page<>(page, limit);
        List<StoreProductRelationQueryVo> storeProductRelationQueryVos = storeProductRelationMapper.selectRelationList(pageModel, uid, type);
        return storeProductRelationQueryVos;

    }
}

